﻿using UnityEngine;
using UnityEngine.UIElements;

public enum NPCState
{
    WAITING,
    EATING,
}

public class NPC : MonoBehaviour
{
    [System.NonSerialized]
    public NPCState currentState = NPCState.WAITING;
    bool isup = false;
    GameObject currentTouch;
    Vector3 oriPos;
    float targetX;
    Animation animation;

    // Start is called before the first frame update
    void Start()
    {
        //oriPos = transform.position;
        animation = GetComponent<Animation>();
    }

    public void SetTarget(int t)
    {
        targetX = 1.5f - t * 0.5f;
    }

    LayerMask LayerMask = 8;
    // Update is called once per frame
    void Update()
    {
        if (currentState == NPCState.WAITING)
        {
            if (isup)
            {
                var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                var cos = Vector3.Dot(ray.direction, Vector3.down);
                var lin = Camera.main.transform.position.y / cos;
                var cro = Camera.main.transform.position + ray.direction * lin;
                var cob = Vector3.Dot(Vector3.up, -ray.direction);
                transform.position = cro - ray.direction * (2 / cob);
                RaycastHit hitinfo;
                if (Physics.Raycast(ray, out hitinfo, Mathf.Infinity, 1 << 8))
                {
                    hitinfo.collider.gameObject.transform.GetChild(0).gameObject.SetActive(true);
                    currentTouch = hitinfo.collider.gameObject;
                    transform.position = currentTouch.transform.position;
                    animation.Play("Sit");
                    transform.rotation = currentTouch.transform.rotation;
                }
                else
                {
                    currentTouch = null;
                    animation.Play("Idle");
                }
            }
            else
            {
                if (transform.position.x < targetX)
                {
                    transform.Translate(Vector3.forward * Time.deltaTime, Space.Self);
                    animation.Play("Walk");
                }
                else
                    animation.Play("Idle");
            }
        }
    }

    private void OnMouseDown()
    {
        if (currentState == NPCState.WAITING)
        {
            isup = true;
            oriPos = transform.position;
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            Vector3 dir = ray.direction;
            var cos = Vector3.Dot(Vector3.up, -dir);
            transform.position = transform.position - dir * (2 / cos);
        }
    }

    private void OnMouseUp()
    {
        if (currentTouch != null)
        {
            transform.position = currentTouch.transform.position;
            GetComponent<Collider>().enabled = false;
            currentTouch.GetComponent<Desk>().StartLoop();
            currentState = NPCState.EATING;
        }
        else
        {
            print("currenttouch==null");
            isup = false;
            transform.position = oriPos;
        }
    }
}
